#include "SequenceItemEditor.h"
#include "ui_SequenceItemEditor.h"

#include "ApplicationIcon.h"

static const int MEDIA_ID = 0; /**< index of media icon in eventTypeCombo */
static const int LIGHT_ID = 1; /**< index of light preset icon in eventTypeCombo */

/* model index is 0 based, view is 1 based */
#define MODEL_TO_VIEW_INDEX( modIndex)   (modIndex + 1)
#define VIEW_TO_MODEL_INDEX( viewIndex)   (viewIndex - 1)

SequenceItemEditor::SequenceItemEditor(QAbstractItemModel *mediaModel,
                                       QAbstractItemModel *lightModel,
                                       QWidget *parent) :
   QWidget(parent),
   ui(new Ui::SequenceItemEditor),
   m_mediaModel(mediaModel),
   m_lightModel(lightModel)
{
   ui->setupUi(this);

   Q_ASSERT( (mediaModel != NULL) && (lightModel != NULL));

   /* keep aligned with constants */
   ui->eventTypeCombo->addItem( QIcon(IconPath("sound.png")), QString(), QVariant( MEDIA_ID));
   ui->eventTypeCombo->addItem( QIcon(IconPath("light.png")), QString(), QVariant( LIGHT_ID));

   ui->eventTypeCombo->setEditable( false);
   ui->targetLabelCombo->setEditable( false);

   connect( ui->eventTypeCombo, SIGNAL(currentIndexChanged(int)),
            this, SLOT(onEventTypeComboChanged(int)) );

   connect( ui->delayManualCheck, SIGNAL(clicked(bool)),
            this, SLOT(onDelayManualCheckClicked(bool)) );

   connect( ui->nextIsFollowingCheck, SIGNAL(clicked(bool)),
            this, SLOT(onNextIsFollowingCheckClicked(bool)) );

   ui->eventTypeCombo->setCurrentIndex( MEDIA_ID);
   ui->targetLabelCombo->setModel( mediaModel);
}

SequenceItemEditor::~SequenceItemEditor()
{
   delete ui;
}

int SequenceItemEditor::nextItemIndex()
{
   int nextItem = VIEW_TO_MODEL_INDEX(ui->nextIndexSpinBox->value());

   if (ui->nextIsFollowingCheck->isChecked())
   {
      nextItem = SequenceItem::NEXT_IS_UNDEFINED;
   }

   return nextItem;
}

void SequenceItemEditor::setNextItemIndex(int index)
{
   if (index != SequenceItem::NEXT_IS_UNDEFINED)
   {
      ui->nextIsFollowingCheck->setChecked( false);
      ui->nextIndexSpinBox->setEnabled( true);
      ui->nextIndexSpinBox->setValue( MODEL_TO_VIEW_INDEX(index));
   }
   else
   {
      ui->nextIsFollowingCheck->setChecked( true);
      ui->nextIndexSpinBox->setEnabled( false);
   }
}

int SequenceItemEditor::delayFromPreviousMs()
{
   int delay = (int)(ui->delaySpinBox->value() * 1000. );

   if (ui->delayManualCheck->isChecked())
   {
      delay = SequenceItem::DELAY_INFINITE;
   }

   return delay;
}

void SequenceItemEditor::setDelayFromPrevious(int millisec)
{
   if (millisec != SequenceItem::DELAY_INFINITE)
   {
      ui->delayManualCheck->setChecked( false);
      ui->delaySpinBox->setValue( ((double)millisec) / 1000.);
      ui->delaySpinBox->setEnabled( true);
   }
   else
   {
      ui->delayManualCheck->setChecked( true);
      ui->delaySpinBox->setEnabled( false);
   }
}

/**
 * selected event type is detected by the current index
 * of target combo.
 */
SequenceItem::eventType SequenceItemEditor::eventType()
{
   SequenceItem::eventType type = SequenceItem::INVALID_EVENT;

   int typeComboIndex= ui->eventTypeCombo->currentIndex();

   if (typeComboIndex == MEDIA_ID)
   {
      type = SequenceItem::TRACK;
   }
   else if (typeComboIndex == LIGHT_ID)
   {
      type = SequenceItem::LIGHT;
   }

   return type;
}

void SequenceItemEditor::setEventType(SequenceItem::eventType type)
{
   if (type == SequenceItem::TRACK)
   {
      ui->eventTypeCombo->setCurrentIndex( MEDIA_ID);
      ui->targetLabelCombo->setModel( m_mediaModel);
   }
   else if (type == SequenceItem::LIGHT)
   {
      ui->eventTypeCombo->setCurrentIndex( LIGHT_ID);
      ui->targetLabelCombo->setModel( m_lightModel);
   }
}

QString SequenceItemEditor::targetLabel()
{
   return ui->targetLabelCombo->currentText();
}

void SequenceItemEditor::setTargetLabel(const QString &label)
{
   ui->targetLabelCombo->setCurrentText( label);
}

void SequenceItemEditor::onEventTypeComboChanged( int index)
{
   if (ui->eventTypeCombo->itemData(index).toInt()  == MEDIA_ID)
   {
      ui->targetLabelCombo->setModel( m_mediaModel);
   }
   else if (ui->eventTypeCombo->itemData(index).toInt()  == LIGHT_ID)
   {
      ui->targetLabelCombo->setModel( m_lightModel);
   }
}

void SequenceItemEditor::onDelayManualCheckClicked(bool newState)
{
   ui->delaySpinBox->setEnabled( !newState);
}

void SequenceItemEditor::onNextIsFollowingCheckClicked(bool newState)
{
   ui->nextIndexSpinBox->setEnabled( !newState);
}

